﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

using MM.Entities;
using MM.Business; 

namespace MM
{
    public partial class frmTimPhieuYC : Form
    {

        // the delegate the subscribers must implement
        public delegate void ChonPhieuYeuCau(object frmTimPhieuYC_KetQua, ChonPhieuYCEventArgs chonPYCevent);

        // an instance of the delegate
        public ChonPhieuYeuCau chonPYC;


        PHIEU_YEU_CAU objPYC = new PHIEU_YEU_CAU();
        List<VAT_TU_PHU_TUNG> lstVTPT = new List<VAT_TU_PHU_TUNG>();
        frmTimKiemVTPT frm;
        frmTimKiemTheoHinh frm1;
        frmImpa_TimKiem frm2;
        frmImpa_TimKiemTheoHinh frm3;
        
        string _sConn = "";
        public frmTimPhieuYC(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
        }

        private void frmTimPhieuYC_Paint(object sender, PaintEventArgs e)
        {
            Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            objPYC.MaPhieuYeuCau = this.txtMaPhieuYeuCau.Text.Trim();
            objPYC.NguoiTao = this.txtNguoiThucHien.Text.Trim();
            objPYC.NgayTao = this.dateEditNgayGiaoHang.DateTime;
            objPYC.NgayTaoTo = this.dateEditNgayGiaoHangTo.DateTime;
            if (this.txtMaVTPT.Text != "")
            {
                VAT_TU_PHU_TUNG oVTPT = new VAT_TU_PHU_TUNG();
                oVTPT._maVTPT = this.txtMaVTPT.Text;
                lstVTPT.Add(oVTPT);
            }
            int daduyet = 2;
            if (radioButton1.Checked == true)
            {
                daduyet = 1;
            }
            if (radioButton2.Checked == true)
            {
                daduyet = 0;
            }
            if (radioButton4.Checked == true)
            {
                objPYC.TinhTrang = 1;
            }

            List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
            clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(_sConn);
            lst=pycCtrl.SelectPHIEU_YEU_CAUsSearch(objPYC,lstVTPT,daduyet);
            if (lst.Count == 0)
                MessageBox.Show("KHÔNG TÌM THẤY PHIẾU YÊU CẦU THỎA ĐIỀU KIỆN");
            else
            {
                this.Hide();
                // GOI FORM KET QUA TIM KIEM                
                frmTimPhieuYC_KetQua f = new frmTimPhieuYC_KetQua(lst, _sConn);
                f.chonPYC += new frmTimPhieuYC_KetQua.ChonPhieuYeuCau(GetPhieuYeuCau);
                f.Location = new Point(
                    (this.Location.X + this.Width / 2 + 100 ) - (f.Width / 2),
                    (this.Location.Y + this.Height / 2 + 100 ) - (f.Height / 2));
                f.StartPosition = FormStartPosition.Manual;                
                f.ShowDialog();

                if (f.fBack == 1)// quay lai form tim kiem
                {
                    this.Show();
                }
                else
                {
                    lstVTPT.Clear();
                    this.Close();
                    this.Dispose();
                }
            }

        }

        private void GetPhieuYeuCau(object sender, ChonPhieuYCEventArgs e)
        {
            objPYC.MaPhieuYeuCau = e.MaPhieuYeuCau;

            ChonPhieuYCEventArgs pycevent = new ChonPhieuYCEventArgs(objPYC.MaPhieuYeuCau);

            if (chonPYC != null)
            {
                chonPYC(this, pycevent);
            }
        }

       
        private void LuuDSVTPT(object sender, lstVtptEventArgs e)
        {
            lstVTPT = e.lstVtpt;
            if (lstVTPT.Count > 0)
            {
                this.txtMaVTPT.Text = lstVTPT[0]._maVTPT;
                this.lblTenVTPT.Text = lstVTPT[0]._tenVTPT;
            }

        }

        private void tìmTheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm = new frmTimKiemVTPT(_sConn, true);
                frm.SelectLstMaterial += new frmTimKiemVTPT.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm.ShowDialog();
        }

        private void tìmTheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstVTPT.Count == 0)
            {
                frm1 = new frmTimKiemTheoHinh(_sConn, true);
                frm1.SelectLstMaterialByImg += new frmTimKiemTheoHinh.SelectListMaterialByImgHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm1.ShowDialog();
        }

        private void tìmVTIMPATheoNhiềuTiêuChíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm2 == null)
            {
                frm2 = new frmImpa_TimKiem(_sConn, true);
                frm2.SelectLstMaterial += new frmImpa_TimKiem.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm2.ShowDialog();
        }

        private void tìmVTIMPATheoHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frm3 == null)
            {
                frm3 = new frmImpa_TimKiemTheoHinh(_sConn, true);
                frm3.SelectLstMaterial += new frmImpa_TimKiemTheoHinh.SelectListMaterialHandler(LuuDSVTPT);
            }
            else
                lstVTPT.Clear();
            frm3.ShowDialog();
        }

        private void frmTimPhieuYC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                this.button1_Click(null, null);
            }
        }
    }


}